/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ntb.controllers;

import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.StampForm;
import com.ntb.entities.StampDutyAndRegistration;
import com.ntb.models.StampDutyAndRegistrationDAO;
import com.ntb.models.impl.StampDutyAndRegistrationDAOImpl;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class StampAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
                        
        ActionForward forward = mapping.getInputForward();
        StampForm stampForm = (StampForm) form;
        StampDutyAndRegistrationDAO stampDao = new StampDutyAndRegistrationDAOImpl();
        String action = stampForm.getAction();

        List<String> errorMessages = new ArrayList();
        List<String> successMessages = new ArrayList();
        if ("save".equalsIgnoreCase(action)) {
            StampDutyAndRegistration stamp = new StampDutyAndRegistration();

            stamp.setId(stampForm.getId());
            stamp.setSdCharge(stampForm.getSdCharge());

            if (stamp.getId() > 0) {
                StampDutyAndRegistration temp = stampDao.getRow(stamp.getId());
                if (temp == null) {
                    errorMessages.add("Record not found!");
                } else {

                        stampDao.update(stamp);
                        successMessages.add("The SD Charge has been saved.");
                        forward = mapping.findForward("stampListing");
                }
            } else {
                    stampDao.add(stamp);
                    successMessages.add("The SD Charge has been added.");
                    forward = mapping.findForward("stampListing");
            }
        } else if (stampForm.getId() > 0) {
            StampDutyAndRegistration stamp = stampDao.getRow(stampForm.getId());

            stampForm.setId(stamp.getId());
            stampForm.setSdCharge(stamp.getSdCharge());
            stampForm.setStartDate(stamp.getStartDate());

            forward = mapping.findForward("stampEdit");
        } else {
            stampForm.setId(0);
        }

        request.setAttribute("errorMessages", errorMessages);
        request.setAttribute("successMessages", successMessages);

        return forward;
    }

}
